package com.tianyu.service.mqtt.handler.inbound.next;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.tianyu.model.dto.DockRealtimeInfoDTO;
import com.tianyu.service.cache.DataCacheService;
import com.tianyu.service.mqtt.common.MqttMessageChannel;
import com.tianyu.service.mqtt.handler.outbound.IMqttOutboundThird01Gateway;
import com.tianyu.service.mqtt.model.dto.HangarStateReceiver;
import com.tianyu.service.mqtt.model.dto.StateResponse;
import com.tianyu.service.tcp.TianyuCompanyMessage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author swyan
 */
@Component
@Slf4j
public class HangarStateHandler {

    @Resource
    private IMqttOutboundThird01Gateway mqttOutboundThird01Gateway;


    @ServiceActivator(inputChannel = MqttMessageChannel.ChannelName.INBOUND_HANGAR_STATE)
    protected void messageReceived(Message<String> message) {
        MessageHeaders headers = message.getHeaders();
        String topic = Objects.requireNonNull(headers.get(MqttHeaders.RECEIVED_TOPIC)).toString();
        final Integer companyId = Integer.parseInt(topic.split("/")[3]);
        final String deviceSn = topic.split("/")[4];
        String content = message.getPayload();
        final JSONObject jsonObject = JSONObject.parseObject(content);
//        log.info(content);
        DataCacheService.dockOsdMap.put(deviceSn,jsonObject);
        // System.out.println(jsonObject);
        final JSONObject tianyuThirdAccessChannel = DataCacheService.tianyuThirdAccessChannelMap.get(companyId);
        if (tianyuThirdAccessChannel == null){
            return;
        }
        final String thirdAccessKey = tianyuThirdAccessChannel.getString("thirdAccessKey");
        String appKey = "c3pfZGFwZW5nQEA6MjAyNDA3MTE";

        final String jsonString = JSONObject.from(StateResponse.newDockState(DockRealtimeInfoDTO.builder()
                .sn(jsonObject.getString("hangarId"))
                .longitude(jsonObject.getDouble("hangarLng"))
                .latitude(jsonObject.getDouble("hangarLat"))
                .aircraftInDock(jsonObject.getInteger("droneInDock"))
                .coverState(jsonObject.getInteger("coverState"))
                .mode(jsonObject.getInteger("modeCode"))
                .accTime(jsonObject.getInteger("accTime"))
                .rainFall(jsonObject.getInteger("rainfall"))
                .windSpeed(jsonObject.getDouble("windSpeed"))
                .temperature(jsonObject.getDouble("temperature"))
                .envTemperature(jsonObject.getDouble("environmentTemperature"))
                .workingElectricity(jsonObject.getDouble("workingCurrent"))
                .workingVoltage(jsonObject.getDouble("workingVoltage"))
                .electricSupplyVoltage(jsonObject.getDouble("electricSupplyVoltage"))
                .upQuality(jsonObject.getDouble("upQuality"))
                .downQuality(jsonObject.getDouble("downQuality"))
                .homeDistance(jsonObject.getDouble("homeDistance"))
                .build())).toJSONString();
         mqttOutboundThird01Gateway.publish("sz/" + thirdAccessKey + "/uav", jsonString.getBytes());
    }
}
